﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;

namespace Omu.Awesome.Mvc
{

    public abstract class MetadataAttribute : Attribute
    {
        protected readonly IList<string> wasset = new List<string>();
        
        public void Process(ModelMetadata m)
        {
            Do(this, m);
        }

        public void Do(object o, ModelMetadata m)
        {
            var props = TypeDescriptor.GetProperties(o);

            for (var i = 0; i < props.Count; i++)
            {
                var p = props[i];

                if (p.GetValue(o) == null
                    || (p.PropertyType == typeof(int) && (int)p.GetValue(o) == 0)
                    || (p.PropertyType == typeof(bool) && !wasset.Contains(p.Name))
                    || p.Name == "TypeId") continue;

                m.AdditionalValues.Add(p.Name, p.GetValue(o));
            }
        }
    }
}